package com.engineering.elink.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.common.ErrorType;
import com.engineering.elink.common.Pagination;
import com.engineering.elink.domain.Video;
import com.engineering.elink.service.ChapterService;
import com.engineering.elink.util.CollectionUtil;
import com.engineering.elink.util.ResJsonUtil;
import com.engineering.elink.videodao.ChapterMapper;
import com.engineering.elink.videodao.VideoMapper;
import com.engineering.elink.vo.ChapterVo;
import com.engineering.elink.vo.VideoVo;

/**
 
 * 章节业务操作实现类
 */
@Service
public class ChapterServiceImpl implements ChapterService{
	
	@Autowired
	private ChapterMapper chapterMapper;
	
	@Autowired
	private VideoMapper videoMapper;
	@Override
	public Map<String, Object> getCourseCatalog(Integer courseId, String userId,Integer freeStatus) {
		
		//获取一个课程下面所有文章，按排序字段进行升序
		List<ChapterVo> pvoList=chapterMapper.selectByCourseId(courseId);
		if(CollectionUtil.collectionIsNotEmpty(pvoList)){
			//获取文章下面的节,按排序字段进行升序
			for(ChapterVo pvo:pvoList){
				List<VideoVo> childChapterList=chapterMapper.selectByChapterId(pvo.getChapterId());
				for(VideoVo vo:childChapterList){
					//获取节下面的视频
					List<Video> videoList=videoMapper.selectVideoByChapterId(vo.getChapterId(),freeStatus);
					vo.setVideoList(videoList);
				}
				pvo.setChildChapterList(childChapterList); 
			}
			
			return ResJsonUtil.getMap(ErrorType.SUCCESS, pvoList);
		}
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}
	
	@Override
	public Map<String, Object> getChapterByCourseId(BaseCo co, String userId,Integer courseId) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("list", chapterMapper.getChapterByCourseId(co, courseId));
		map.put("Pagination",new Pagination(co, chapterMapper.countChapterByCourseId(courseId)));
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, map);
	}
}
